package com.gzhu.knowledgeAdmin.repository;


import com.gzhu.knowledgeAdmin.entity.Flow;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 继承JpaRepository<Flow,Integer>
 *     Flow：要操作的表格对象
 *     Integer：主键 ID 的数据类型
 *
 *     继承的这个类里面封装了基本的增删改查方法直接调用即可，
 *     如果集成的方法没法满足需求，可以在此封装,但是一定要注意 “命名规范”
 */
@Repository(value = "flowRepository")
public interface FlowRepository extends JpaRepository<Flow,Integer> {
    /**
     * 根据templateId获取他对应的流程flow
     * @param templateId 话术模板id
     * @return list
     */
    List<Flow> getByTemplateId(int templateId);

    /**
     * 根据名称获取对应分类下的flow
     * @param name
     * @return
     */
    List<Flow> getByName(String name);

    /**
     * 根据name和gsid删除flow
     * @param name
     * @return
     */
    @Modifying
    @Transactional
    @Query("delete from Flow where name = ?1 and gsid = ?2")
    int deleteFlowByNameAndGSID(String name,String GSID);

    /**
     * 根据名称和gsid获取对应下的flow
     * @param name
     * @return
     */
    @Query(value = "select * from T_HSLC_T where name = ?1 and f_gsid = ?2",nativeQuery = true)
    List<Flow> getFlowByNameAndGSID(String name, String GSID);

    /**
     * 根据templateId和answerType获取对应的流程Flow
     * @param templateId
     * @param answerType
     * @return
     */
    Flow getByTemplateIdAndAnswerType(int templateId,String answerType);

    /**
     * 根据templateId和answerTarget获取flow
     * @param templateId
     * @param answerTarget
     * @return
     */
    Flow getFlowByTemplateIdAndAnswerTarget(int templateId,int answerTarget);

    /**
     * 根据templateId和answerType几个获取对应的流程Flow
     * @param templateId
     * @param answerTypeList
     * @return
     */
    List<Flow> getByTemplateIdAndAnswerTypeIn(int templateId,List<String> answerTypeList);

    /**
     * 根据answerTarget获取到对应的流程Flow
     * @param answerTarget
     * @return
     */
    Flow getFlowByAnswerTarget(int answerTarget);

    /**
     * 用于判断templateId下,除了跳转到answerTarget的flow,是否已经存在其他跳转条件为answerType的flow
     * sql: select count(*) from flow where templateId = :templateId and answerType = :answerType and answerTarget != :answerTarget
     * @param templateId
     * @param answerType
     * @param answerTarget
     * @return
     */
    int countByTemplateIdAndAnswerTypeAndAnswerTargetNot(Integer templateId,String answerType,Integer answerTarget);

    /**
     * 根据answerTarget修改templateId
     * @param answerId
     * @param templateId
     * @return
     */
    @Modifying
    @Transactional
    @Query("update Flow set templateId = ?2 where answerTarget = ?1")
    int updateTemplateIdByTargetId(Integer answerId,Integer templateId);

    /**
     * 根据name,修改name
     * @param originalName 原始的name
     * @param name 修改后的name
     * @return 返回受影响的行数
     */
    @Modifying
    @Transactional
    @Query("update Flow set name = ?2 where name = ?1")
    int updateName(String originalName,String name);

    /**
     * 根据name,修改name
     * @param originalName 原始的name
     * @param name 修改后的name
     * @return 返回受影响的行数
     */
    @Modifying
    @Transactional
    @Query("update Flow set name = ?3 where name = ?1 and gsid = ?2")
    int updateName(String originalName,String GSID,String name);

    /**
     * 修改beforeId的flow的指向未新的话术id
     * @param beforeId
     * @param afterId
     * @param newAnswerTarget
     * @return
     */
    @Modifying
    @Transactional
    @Query("update Flow set answerTarget = ?3 where templateId = ?1 and answerTarget = ?2")
    int updateAnswerTargetByBeforeIdAndAfterId(int beforeId,int afterId,int newAnswerTarget);

    /**
     * 获取指向parentId的flow
     * 插入尾部话术时,获取逻辑顺序
     * 若获取到的数据为空,则设置为1
     * 若不为空,则设置为获取到的数据 + 1
     * @param parentId
     * @return
     */
    @Query(value = "select * from T_HSLC_T where answer_target = ?1",nativeQuery = true)
    Flow getPreFlow(Integer parentId);

    /**
     * 根据ids,把flow中的逻辑顺序logicOrder + 1
     * @param ids
     * @return
     */
    @Modifying
    @Transactional
    @Query("update Flow set logicOrder = logicOrder + 1 where templateId in ?1")
    int updateLogicOrderByTemplateIds(List<Integer> ids);

    /**
     * 根据name删除flow
     * @param name
     * @return
     */
    int deleteFlowByName(String name);

    /**
     * 根据idList,删除所有指向的flow
     * @param idList
     * @return
     */
    int deleteFlowByAnswerTargetIn(List<Integer> idList);

    /**
     * 根据name集合,获取到所对应的Flow
     * @param nameList
     * @return
     */
    List<Flow> getFlowByNameIn(List<String> nameList);

    @Modifying
    @Transactional
    int deleteFlowByTemplateIdAndAnswerTarget(Integer templateId, Integer answerTarget);
}
